<template>
  <el-dialog
    :model-value="UserVisible"
    title="工单分配"
    width="65%"
    top="10px"
    @close="handleClose"
  >
    <el-row :gutter="20" class="header">
      <el-col :span="24">
        <el-input placeholder="输入真实姓名" clearable v-model="queryForm.nickname"></el-input>
      </el-col>
    </el-row>
    <el-row class="header">
      <el-table ref="multipleTable" :data="tableData" row-key border style="width: 100%" @select="select"
                select-on-indeterminate="true">
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="email" label="邮箱" width="150px" />
        <el-table-column prop="phone" label="电话" />
        <el-table-column prop="nickname" label="真实姓名" />
        <el-table-column prop="gender" label="性别" :formatter="formatSex" />
        <el-table-column prop="icon" label="头像" />
        <el-table-column prop="storeId" label="门店id" />
        <el-table-column prop="role.name" label="角色类型" />
        <el-table-column prop="status" label="状态">
          <template #default="{ row }">
            <el-tag type="success" effect="dark" v-if="row.status==1">启用</el-tag>
            <el-tag type="warning" effect="dark" v-if="row.status==0">停用</el-tag>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-pagination
      v-model:current-page="queryForm.pageNum"
      v-model:page-size="queryForm.pageSize"
      :page-sizes="[10, 20, 30, 50]"
      layout="prev, pager, next, jumper, sizes, total"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :background="true"
    />
    <div style="margin-top: 20px">
      <el-button type="primary" @click="handleConfirm">确认</el-button>
    </div>
  </el-dialog>
</template>

<script setup>
import { ref, onMounted, defineEmits, defineProps, watch } from 'vue'
import { repair, userRole } from '../../../api/workorder'
import { ElMessage } from 'element-plus'

const queryForm = ref({
  id: '',
  userId: '',
  nickname: '',
  pageNum: 1,
  pageSize: 10
})

const form = ref({
  id: '',
  recordId: '',
  userId: ''
})

const tableData = ref([])
const total = ref(0)

async function query() {
  const resp = await userRole(queryForm.value)
  tableData.value = resp.data.data.list
  total.value = resp.data.data.total
}

// 监听搜索框回车事件
const keyDown = (e) => {
  // 如果是回车则执行登录方法
  if (e.keyCode === 13) {
    query()
  }
}

// 多选框拿数据
function select(selection, row) {
  console.log(row)
  queryForm.value = JSON.parse(JSON.stringify(row))
  form.value.userId = queryForm.value.id
}

// 确定
async function handleConfirm() {
  await repair(form.value)
  ElMessage({
    message: '更新成功',
    type: 'success'
  })
  // 上层组件重新获取数据
  emits('query')
  handleClose()
}

const props = defineProps({
  // 标题
  dialogTitle: {
    type: String,
    default: '',
    required: true
  },
  // 编辑行数据
  dialogRowValue: {
    type: Object,
    default: () => {}
  }
})
// 监听props.dialogRowValue值改变
watch(
  () => props.dialogRowValue,
  () => {
    form.value = props.dialogRowValue
  },
  { deep: true, immediate: true }
)

// 修改其他属性的值
const emits = defineEmits(['update:modelValue', 'query'])
// 关闭
function handleClose() {
  emits('update:modelValue', false)
}

onMounted(() => {
  window.addEventListener('keydown', keyDown)
  query()
})
window.removeEventListener('keydown', keyDown, false)

function formatSex(row, column) {
  const sex = row[column.property]
  if (sex === 'M') {
    return '男'
  } else {
    return '女'
  }
}

function handleCurrentChange(pageNum) {
  queryForm.value.pageNum = pageNum
  query()
}
function handleSizeChange(pageSize) {
  queryForm.value.pageNum = 1
  queryForm.value.pageSize = pageSize
  query()
}
</script>

<style lang="scss" scoped>
.header {
  padding-bottom: 16px;
  box-sizing: border-box;
}
::v-deep .el-input__suffix {
  align-items: center;
}
</style>
